package com.evaluation.kexingcp.psycho.evaluation.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.evaluation.kexingcp.admin.api.entity.SysUser;
import com.evaluation.kexingcp.admin.service.SysUserService;
import com.evaluation.kexingcp.common.core.util.R;
import com.evaluation.kexingcp.common.excel.annotation.ResponseExcel;
import com.evaluation.kexingcp.common.log.annotation.SysLog;
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationFactorRecordsEntity;
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationFactorRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 测评记录
 *
 * @author pig
 * @date 2024-07-12 09:48:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/evaluationFactorRecords" )
@Tag(description = "evaluationFactorRecords" , name = "测评记录管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EvaluationFactorRecordsController {

    private final  EvaluationFactorRecordsService evaluationFactorRecordsService;
    private final SysUserService sysUserService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param evaluationFactorRecords 测评记录
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationFactorRecords_view')" )
    public R getEvaluationFactorRecordsPage(@ParameterObject Page page, @ParameterObject EvaluationFactorRecordsEntity evaluationFactorRecords) {
        return R.ok(evaluationFactorRecordsService.page(page, evaluationFactorRecords));
    }


    /**
     * 通过id查询测评记录
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationFactorRecords_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(evaluationFactorRecordsService.getById(id));
    }

    /**
     * 新增测评记录
     * @param evaluationFactorRecords 测评记录
     * @return R
     */
    @Operation(summary = "新增测评记录" , description = "新增测评记录" )
    @SysLog("新增测评记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationFactorRecords_add')" )
    public R save(@RequestBody EvaluationFactorRecordsEntity evaluationFactorRecords) {
        return R.ok(evaluationFactorRecordsService.save(evaluationFactorRecords));
    }

    /**
     * 修改测评记录
     * @param evaluationFactorRecords 测评记录
     * @return R
     */
    @Operation(summary = "修改测评记录" , description = "修改测评记录" )
    @SysLog("修改测评记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationFactorRecords_edit')" )
    public R updateById(@RequestBody EvaluationFactorRecordsEntity evaluationFactorRecords) {
        return R.ok(evaluationFactorRecordsService.updateById(evaluationFactorRecords));
    }

    /**
     * 通过id删除测评记录
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除测评记录" , description = "通过id删除测评记录" )
    @SysLog("通过id删除测评记录" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationFactorRecords_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(evaluationFactorRecordsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param evaluationFactorRecords 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pigx_evaluationFactorRecords_export')" )
    public List<EvaluationFactorRecordsEntity> export(EvaluationFactorRecordsEntity evaluationFactorRecords,Long[] ids) {
        return evaluationFactorRecordsService.list(Wrappers.lambdaQuery(evaluationFactorRecords).in(ArrayUtil.isNotEmpty(ids), EvaluationFactorRecordsEntity::getId, ids));
    }
}